/*
 *******************
 ******************************* C HEADER FILE *******************************
 ** 						  *******************						       **
 ** 																		   **
 ** project : ClockSync 											           **
 ** filename :Timer															   **
 ** version : 1 															   **
 ** date : -	2012.04.10.													   **
 ** 																		   **
 *****************************************************************************
 ** 																		   **
 ** Copyright (c) 2012,  						  				               **
 ** All rights reserved.													   **
 ** 																		   **
 *****************************************************************************
 VERSION HISTORY:
 ----------------
 Version : 1
 Date : -
 Revised by : -
 Description : Original version.
 */
#ifndef _USERTASKS_H_INCLUDED
#define _USERTASKS_H_INCLUDED

/****************************************************************************/
/** 																	   **/
/** 							MODULES USED 							   **/
/** 																	   **/
/****************************************************************************/

/****************************************************************************/
/** 																	   **/
/** 							DEFINITIONS AND MACROS 					   **/
/** 																	   **/
/****************************************************************************/
/* Unitialized timer value */
#define ERROR_INIT_VALUE (1<<30)

/* tasks`s stack size are the same */
#define TASK_STACK_SIZE 1024

/* thr1 prior */
#define TIMED_CLOCK_SYNC_REQUEST_PRIOR 11

/* thr2 prior */
#define TIMED_CLOCK_SYNC_ANSWER_PRIOR 12

/* thr3 prior */
#define COMMON_RECIEVE_PRIOR 13

/* the count of the nodes in the network ID-s are  1 - 4*/
#define NODE_COUNT 8

/* one device`s request time */
#define REQUEST_TIME_SLOT 10000000

/* one device`s answer time */
#define ANSWER_TIMES_SLOT (REQUEST_TIME_SLOT / NODE_COUNT)

/* sizeof RF packet */
#define PACKET_SIZE sizeof(Packet_t)
/****************************************************************************/
/** 																	   **/
/** 							TYPEDEFS AND STRUCTURES 				   **/
/** 		5															   **/
/****************************************************************************/
/* struct type for the clocks attributes */
typedef struct
{
	/* clock uncertainty at last synchronization time */
	cyg_int32 H;

	/* last synchronization time */
	cyg_uint32 Rho;

	/* clock drift */
	float Drift;

	/* time when the last request sent */
	cyg_uint32 Request_sent_time;

	/* Till uninitialized while not synchronized with the other devices */
	cyg_bool Is_initialized;

} SyncClock_t;

/* Flags setted from timer dsr-s */
typedef enum
{
	TIMED_CLOCK_SYNC_REQUEST_EVENT = 0x1, TIMED_CLOCK_SYNC_ANSWER_EVENT = 0x2
} Events_t;

/* RF message type */
typedef enum
{
	CLOCK_REQUEST = 0, CLOCK_ASNWER
} MessageType_T;

/* RF Message */
typedef struct
{
	/* Message sender */
	cyg_uint8 SourceId;

	/* Message address */
	cyg_uint8 DestinationID;

	/* Message type time-request(CLOCK_REQUEST) / answer for request (CLOCK_ASNWER)*/
	MessageType_T MessageType;

	/* Sender clock */
	cyg_uint32 Clock;

	/* Sender`s clock error */
	cyg_int32 ClockError;

} Packet_t;

/****************************************************************************/
/** 																	   **/
/** 							EXPORTED VARIABLES						   **/
/** 																	   **/
/****************************************************************************/
#ifndef _USERTASKS_C_SRC

/* the device-s NODE-id*/
cyg_uint8 NODE_ID;

/* answ timer or request timer elapsed event flag */
cyg_flag_t Timed_clock_sync_event;

cyg_mutex_t Com_layer_send_mutex;

/********************************************************************
 * 		THR1 - TIMED CLOCK SYNC REQUEST
 ********************************************************************/

/* thr1  timed_clock_sync_request */
int Timed_clock_sync_request_stack[TASK_STACK_SIZE];
cyg_handle_t Timed_clock_sync_request_handle;
cyg_thread Timed_clock_sync_request_request_obj;

/********************************************************************
 * 		THR2 - TIMED CLOCK SYNC ANSWER
 ********************************************************************/
/* thr2 timed_clock_sync_answer*/
int Timed_clock_sync_answer_stack[TASK_STACK_SIZE];
cyg_handle_t Timed_clock_sync_answer_handle;
cyg_thread Timed_clock_sync_answer_obj;

/********************************************************************
 * 		THR3 - COMMON RECIEVE
 ********************************************************************/

/* thr3 common_recieve thread */
int Common_recieve_stack[TASK_STACK_SIZE];
cyg_handle_t Common_recieve_handle;
cyg_thread Common_recieve_obj;

#endif

/****************************************************************************/
/** 																	   **/
/** 							EXPORTED FUNCTIONS 						   **/
/** 																	   **/
/****************************************************************************/
/* Task which request the other devices clock periodicatlly */
void Timed_clock_sync_request(cyg_addrword_t);

/* Task which answer`s the other devices clock request */
void Timed_clock_sync_answer(cyg_addrword_t);

/* Task which reads the data from the radio-module */
void Common_recieve(cyg_addrword_t);

/* Initialize the syncron clock to it`s default value */
void Init_sync_clcok(void);

#endif
/****************************************************************************/
/** 																	   **/
/** 							EOF 							   		   **/
/** 																	   **/
/****************************************************************************/
